package com.imagination.applet.domain.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;

import java.net.URL;
import java.util.Date;

public class OssUtil {

    private static OSS oss = SpringUtil.getAppContext().getBean("oss",OSS.class);


    /**
     * 获取临时路径
     * @param key
     * @return
     */
    public static String getTempUrl(String key){
        if(key==null||key.equals("")){
            return null;
        }
        // 设置URL过期时间，例如设置为一小时后过期
        Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
        if(key.startsWith("/")||key.startsWith("\\")){
            key = key.substring(1);
        }

        // 创建GeneratePresignedUrlRequest对象
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest("voyagerke-test", key);
        request.setExpiration(expiration);

        // 生成临时访问路径
        URL signedUrl = oss.generatePresignedUrl(request);
        return signedUrl.toString();
    }
}
